package com.letoken.platform.app.job;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.letoken.platform.app.service.*;
import com.letoken.platform.pub.enums.LetokenTokenTypeEnum;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.repo.admin.dao.AdminIssueConfigMapper;
import com.letoken.platform.repo.admin.po.AdminIssueConfig;
import com.letoken.platform.repo.app.dao.AppUserIssueMapper;
import com.letoken.platform.repo.app.dao.AppUserIssueRecordMapper;
import com.letoken.platform.repo.app.dao.AppUserTeamMapper;
import com.letoken.platform.repo.app.dao.AppUserWalletMapper;
import com.letoken.platform.repo.app.po.*;
import com.letoken.platform.repo.app.res.config.QueryAppIssueConfigRes;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;


@Component
public class SharingCirculationRewardsJbo {

    @Autowired
    private AppUserIssueService appUserIssueService;

    @Resource
    private AppUserIssueMapper appUserIssueMapper;

    @Resource
    private AdminIssueConfigMapper adminIssueConfigMapper;
    @Resource
    private AppUserWalletMapper appUserWalletMapper;

    @Autowired
    private AppUserIssueHistoryService appUserIssueHistoryService;

    @Autowired
    private AppUserLogService appUserLogService;

    @Resource
    private AppUserTeamMapper appUserTeamMapper;

    @Resource
    private AppUserIssueRecordMapper appUserIssueRecordMapper;


    // @Scheduled(cron = "0 */1 * * * ?")
    @XxlJob("SharingCirculationRewards")
    public synchronized void reward() {

        List<AppUserIssue> list = appUserIssueMapper.getWaitRewardList();
        if (list == null || list.size() < 0) {
            return;
        }
        QueryAppIssueConfigRes queryLetokenIssueConfig = adminIssueConfigMapper.queryLetokenIssueConfig();
        BigDecimal rate = new BigDecimal(queryLetokenIssueConfig.getCirculateRate()).divide(new BigDecimal("100"));
        LocalDateTime time = LocalDateTime.now();
        for (AppUserIssue appUserIssue : list) {
            String uuid = UUID.randomUUID().toString();
            QueryWrapper<AppUserWallet> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", appUserIssue.getUserId()).eq("token_id", 2);
            AppUserWallet userWallet = appUserWalletMapper.selectOne(queryWrapper);
            if (userWallet == null) {
                continue;
            }
            BigDecimal amount = appUserIssueService.circulate(appUserIssue, 4, 1, queryLetokenIssueConfig.getCirculateDuration() * 24 * 60, rate, uuid, null, null, null);
            AppUserIssueHistory appUserIssueHistory = new AppUserIssueHistory();
            appUserIssueHistory.setUserId(appUserIssue.getUserId());
            appUserIssueHistory.setType(4);
            appUserIssueHistory.setAmount(amount.negate());
            appUserIssueHistory.setUuid(uuid);
            appUserIssueHistory.setCreateTime(time);
            appUserIssueHistoryService.save(appUserIssueHistory);
        }
    }


    @XxlJob("updateTeamMaxAccelerate")
    public void updateTeamMaxAccelerate() {
        appUserTeamMapper.updateTeamMaxAccelerate();
    }

    @XxlJob("update")
    public void update(){
        appUserIssueService.circulatesss();

    }
}
